package com.zcsy.commons.tools.highchart;

import java.util.List;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.zcsy.commons.tools.highchart.model.BaseChartInfo;
import com.zcsy.commons.tools.highchart.model.LineDataInfo;
import com.zcsy.commons.tools.highchart.model.PieChartInfo;
import com.zcsy.commons.tools.highchart.model.PieDataInfo;
import com.zcsy.commons.tools.highchart.model.common.Chart;
import com.zcsy.commons.tools.highchart.model.common.TitleInfo;
import com.zcsy.commons.tools.highchart.model.common.Tooltip;
import com.zcsy.commons.tools.highchart.model.common.XAxisInfo;
import com.zcsy.commons.tools.highchart.model.common.YAxisInfo;

/**  
 * @Project: common
 * @Title: HighChartTool.java
 * @Package com.zcsy.commons.tools.highchart
 * @Description: TODO
 *
 * @author xiangjw 303310059@qq.com
 * @date 2016年3月18日 上午11:27:09
 * @Copyright: 2016 武汉中财数元信息技术有限公司 All rights reserved.
 * @version V1.0  
 */
public class HighChartTool {
	
	/**
	 * 区域图
	 */
	public static final String CHART_AREA = "area";

	/**
	 * 柱状图
	 */
	public static final String CHART_COLUMN = "column";
	
	/**
	 * 折线图
	 */
	public static final String CHART_LINE = "line";
	
	/**
	 * 饼状图
	 */
	public static final String CHART_PIE = "pie";
	
	/** 
	 * @Title: getBaseLineChart 
	 * @Description: 初始化图数据,包含折线图、柱状图、区域图
	 * @param @param chartType 类型，见上面，默认为折线图
	 * @param @param title 标题
	 * @param @param subtitle 副标题,可传空
	 * @param @param unit 数据的单位,可传空
	 * @param @param xTitles 横坐标，各单元格意义
	 * @param @param yTitle 纵坐标，值得意义
	 * @param @param data 数据值
	 * @param @return     
	 * @return String     初始化好的数据字符串，直接在js中$('#container').highcharts(${line});即可
	 * @throws 
	 */
	public static String getBaseChart(String chartType , String title , String subtitle , 
			String unit , String[] xTitles , String yTitle , List<LineDataInfo> data
			){
		BaseChartInfo line = new BaseChartInfo();
		line.setChart(new Chart(chartType));
		line.setTitle(new TitleInfo(title, -20));
		line.setSubtitle(new TitleInfo(subtitle, -20));
		line.setTooltip(new Tooltip(unit));
		line.setxAxis(new XAxisInfo(xTitles));
		line.setyAxis(new YAxisInfo(new TitleInfo(yTitle, 0)));
		line.setSeries(data);
		
		return JSONObject.toJSONString(line);
	}
	
	
	/** 
	 * @Title: getBasePieChart 
	 * @Description: 初始化饼状图数据
	 * @param @param title 标题
	 * @param @param unit 单位，可传空
	 * @param @param subtitle 副标题,可传空
	 * @param @param data 数据值
	 * @param @return     
	 * @return String   初始化好的数据字符串，直接在js中$('#container').highcharts(${pie});即可 
	 * @throws 
	 */
	public static String getBasePieChart(String title , String unit , String subtitle 
			, List<PieDataInfo> data){
		PieChartInfo line = new PieChartInfo();
		line.setChart(new Chart(CHART_PIE));
		line.setTitle(new TitleInfo(title, -20));
		line.setSubtitle(new TitleInfo(subtitle, -20));
		if(StringUtils.isEmpty(unit)){
			line.setTooltip(new Tooltip(null , "{point.y}: <b>{point.percentage:.1f}%</b>"));
		}else{
			line.setTooltip(new Tooltip(null , "{point.y}" + unit + ": <b>{point.percentage:.1f}%</b>"));
		}
		
		line.setSeries(data);
		
		return JSONObject.toJSONString(line);
	}
	
	public static void main(String[] args){
		
	}
}
